﻿using BrnMall.Manager.Domain.User;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.User
{
    public static class CashRecordManager
    {
        public static bool Add(CashRecord model)
        {
            object obj = null;
            using (var db = new QDMallDatabase())
            {
                obj = db.Insert(model);
            }
            if (obj != null)
                return true;
            return false;
        }

        /// <summary>
        /// 获取充值记录
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="direction">资金流向 (1 收入,2 支出)</param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static Page<CashRecord> GetList(int uid, int direction, int page, int pageSize = 15)
        {
            var sql = new StringBuilder();
            sql.AppendFormat("select * from cash_CashRecord where uid={0}", uid);

            if (direction > 0)
                sql.AppendFormat(" and direction={0}", direction);
            sql.Append(" order by id desc");

            using (var db = new QDMallDatabase())
            {
                return db.Page<CashRecord>(page, pageSize, sql.ToString());
            }
        }


        public static Page<CashRecord> GetListPage(int storeId,int Uid, string nickName, int direction, string startTime, string endTime, int page, int pageSize = 15)
        {
            var sql = new StringBuilder();
            sql.AppendFormat("select * from cash_CashRecord where storeId={0}", storeId);

            //收入支出
            if (direction > 0)
                sql.AppendFormat(" and direction={0}", direction);

            if (Uid > 0)
                sql.AppendFormat(" and Uid={0}", Uid);

            //昵称查询
            if (!string.IsNullOrEmpty(nickName))
            {
                var uid = UsersManager.GetIdsByNickNameToString(nickName, 20);
                if (uid.IndexOf(",") > -1)
                    sql.AppendFormat(" and uid in({0})", uid);
                else 
                    sql.AppendFormat(" and uid={0}", uid);
            }

            if (!string.IsNullOrEmpty(startTime))
                sql.AppendFormat(" and CreateTime>='{0}'", startTime);

            if (!string.IsNullOrEmpty(endTime))
                sql.AppendFormat(" and CreateTime<'{0}'", Convert.ToDateTime(endTime).AddDays(1));

            sql.Append(" order by id desc");

            using (var db = new QDMallDatabase())
            {
                return db.Page<CashRecord>(page, pageSize, sql.ToString());
            }
        }

        public static Dictionary<string, decimal> GetTotalMoney(int storeId, int uid)
        {
            return GetTotalMoney(storeId, uid, string.Empty, string.Empty, string.Empty);
        }

        public static Dictionary<string, decimal> GetTotalMoney(int storeId, int uid, string nickName, string startTime, string endTime)
        {
            Dictionary<string, decimal> dic = new Dictionary<string, decimal>();
            dic["TimeOutMoney"] = 0;   //总支出
            dic["TimeInMoney"] = 0;    //总收入

            var sql = new StringBuilder();
            sql.AppendFormat(" and storeId={0}", storeId);
            if (uid > 0)
                sql.AppendFormat(" and uid={0}", uid);

            //昵称查询
            if (!string.IsNullOrEmpty(nickName))
            {
                var uids = UsersManager.GetIdsByNickNameToString(nickName, 20);
                if (uids.IndexOf(",") > -1)
                    sql.AppendFormat(" and uid in({0})", uids);
                else
                    sql.AppendFormat(" and uid={0}", uids);
            }

            if (!string.IsNullOrEmpty(startTime))
                sql.AppendFormat(" and CreateTime>='{0}'", startTime);

            if (!string.IsNullOrEmpty(endTime))
                sql.AppendFormat(" and CreateTime<'{0}'", Convert.ToDateTime(endTime).AddDays(1));

            using (var db = new QDMallDatabase())
            {
                object outMoney = db.ExecuteScalar<object>(string.Format("select SUM(amount) as totalMoney from cash_cashrecord where direction=2 {0}", sql.ToString()));
                object inMoney = db.ExecuteScalar<object>(string.Format("select SUM(amount) as totalMoney from cash_cashrecord where direction=1 {0}", sql.ToString()));
                dic["TimeOutMoney"] = outMoney == DBNull.Value ? 0 : Convert.ToDecimal(outMoney);
                dic["TimeInMoney"] = inMoney == DBNull.Value ? 0 : Convert.ToDecimal(inMoney);
            }
            return dic;
        }
    }
}